package com.zys.http.ui.tab.panel;

import cn.hutool.core.text.CharSequenceUtil;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBPanel;
import com.intellij.util.ui.JBUI;
import com.zys.http.action.CommonAction;
import com.zys.http.constant.HttpEnum;
import com.zys.http.entity.ReqHistory;
import com.zys.http.entity.param.ParamProperty;
import com.zys.http.extension.service.Bundle;
import com.zys.http.tool.ui.ComboBoxTool;
import com.zys.http.ui.dialog.EditorDialog;
import com.zys.http.ui.editor.CustomEditor;
import com.zys.http.ui.icon.HttpIcons;
import jdk.jfr.Description;
import lombok.Getter;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

/**
 * @author zys
 * @since 2025-02-19
 */
@Getter
public class ResponsePanel extends JBPanel<RequestFilePanel> implements TabOperation {
    private static final String REQUEST_RESULT_TEXT = "STATUS: {}";
    @Description("响应体类型")
    private final CustomEditor responseEditor;
    @Description("请求结果文本")
    private final JLabel requestResult = new JLabel();

    public ResponsePanel(Project project, boolean resTabNeedFileType) {
        super(new BorderLayout(0, 0));
        this.setBorder(JBUI.Borders.customLine(JBColor.PanelBackground, 0, 3, 0, 0));
        responseEditor = new CustomEditor(project);
        this.add(responseEditor, BorderLayout.CENTER);

        JPanel respExpandPanel = new JPanel(new BorderLayout(0, 0));
        CommonAction action = new CommonAction(Bundle.get("http.api.tab.response.action.view"), "", HttpIcons.General.FULL_SCREEN);
        action.setAction(e -> new EditorDialog(project, Bundle.get("http.api.tab.response.action.view.dialog"),
                responseEditor.getFileType(), responseEditor.getText()).show());
        DefaultActionGroup group = new DefaultActionGroup();
        group.add(action);
        ActionToolbar component = (ActionToolbar) ActionManager.getInstance()
                .createActionToolbar("http.body.editor", group, true).getComponent();
        component.setReservePlaceAutoPopupIcon(false);
        component.setTargetComponent(responseEditor);
        if (resTabNeedFileType) {
            JLabel label = new JLabel(Bundle.get("http.api.tab.response.type.label"));
            ComboBox<FileType> comboBox = ComboBoxTool.fileTypeComboBox(itemListener(responseEditor));
            respExpandPanel.add(label, BorderLayout.WEST);
            respExpandPanel.add(comboBox, BorderLayout.CENTER);
        } else {
            respExpandPanel.add(requestResult, BorderLayout.WEST);
        }
        respExpandPanel.add((JComponent) component, BorderLayout.EAST);
        this.add(respExpandPanel, BorderLayout.SOUTH);
    }

    @Override
    public void reset() {
        this.responseEditor.setText("");
    }

    @Override
    public void execute(HttpEnum.HttpMethod httpMethod, HttpEnum.ContentType contentType, String param, Map<String, ParamProperty> paramPropertyMap) {

    }

    @Override
    public void recover(ReqHistory history) {
        this.responseEditor.setText(history.getRes());
    }

    public void set404Text(String text) {
        this.responseEditor.setText(text, ComboBoxTool.TEXT_FILE_TYPE);
        this.resultText(404);
    }

    public void resultText(int status) {
        requestResult.setText(CharSequenceUtil.format(REQUEST_RESULT_TEXT, status));
        if (status >= 200 && status < 300) {
            // 成功
            requestResult.setForeground(JBColor.GREEN);
        } else if (status >= 300 && status < 400) {
            // 重定向
            requestResult.setForeground(JBColor.YELLOW);
        } else {
            // 错误
            requestResult.setForeground(JBColor.RED);
        }
    }

    public void setResultTextWithMs(long ms) {
        this.requestResult.setText(requestResult.getText() + ", " + ms + "ms");
    }
}
